package com.dxx.cloud.admin.constant.enums;

import lombok.Getter;

import java.time.Duration;

@Getter
public enum CacheKeyEnum {
    /**
     * 登录验证码
     */
    LOGIN(ModuleEnums.COMMON,"SMSValidCode", false, Duration.ofMinutes(1),"登录验证码"),
    ;

    private ModuleEnums module;
    /**
     * keyid
     */
    private final String keyPrefix;
    /**
     * 是否使用项目前缀
     */
    private final boolean isPrefix;
    /**
     * 缓存时长
     */
    private final Duration duration;
    private String remark;

    CacheKeyEnum(String key, boolean isPrefix, Duration duration) {
        this.keyPrefix = key;
        this.isPrefix = isPrefix;
        this.duration = duration;
    }

    CacheKeyEnum(ModuleEnums module,String key, boolean isPrefix, Duration duration,String remark) {
        this.module= module;
        this.keyPrefix = key;
        this.isPrefix = isPrefix;
        this.duration = duration;
        this.remark = remark;
    }

    public static CacheKeyEnum convert(String name) {
        try {
            return CacheKeyEnum.valueOf(name);
        } catch (Exception e) {
            return null;
        }
    }

    public String toKey(Object... key) {
        StringBuilder keyBuilder = new StringBuilder();
        if (this.isPrefix) {
            keyBuilder.append("admin").append(":");
        }
        keyBuilder.append(getKeyPrefix());
        for (Object s : key) {
            keyBuilder.append(":").append(s);
        }
        return keyBuilder.toString();
    }
}
